package com.newdjk.newdoctor.ui;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.google.gson.Gson;
import com.newdjk.newdoctor.BasActivity;
import com.newdjk.newdoctor.MainActivity;
import com.newdjk.newdoctor.MyApplication;
import com.newdjk.newdoctor.R;
import com.newdjk.newdoctor.base.BaseAppcompatActivity;
import com.newdjk.newdoctor.net.BaseComPleteObserver;
import com.newdjk.newdoctor.net.NetServices;
import com.newdjk.newdoctor.utils.Contants;
import com.newdjk.newdoctor.utils.SpUtils;
import com.newdjk.newdoctor.utils.StrUtil;
import com.newdjk.okhttp.RetrofitFactory;
import com.newdjk.okhttp.entity.BaseEntity;
import com.newdjk.okhttp.entity.Login;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class SplashActivity extends BaseAppcompatActivity {
    public static final int REQUEST_CODE = 111;
    private static final String TAG = "SplashActivity";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        setDarkStatusWhite(true);
        //checkPermission();
        boolean isfirstuse = SpUtils.getBoolean(Contants.IS_FIRST_USE, true);
        if (isfirstuse) {
            startActivity(new Intent(SplashActivity.this, GuideActivity.class));
            finish();
        } else {
            //判断token是否过期
            isTokenExpired();
        }

    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private boolean checkPermission() {

        List<String> permissions = new ArrayList<>();
        if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(MyApplication.getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
        if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(MyApplication.getContext(), Manifest.permission.CAMERA)) {
            permissions.add(Manifest.permission.CAMERA);
        }

        if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(MyApplication.getContext(), Manifest.permission.READ_EXTERNAL_STORAGE)) {
            permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
        }
        if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(MyApplication.getContext(), Manifest.permission.ACCESS_FINE_LOCATION)) {
            permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
        }
        if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(MyApplication.getContext(), Manifest.permission.ACCESS_COARSE_LOCATION)) {
            permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
        }

        int len = permissions.size();
        if (len != 0) {
            String[] per = new String[len];
            for (int i = 0; i < len; i++) {
                per[i] = permissions.get(i);
            }
            requestPermissions(
                    per,
                    REQUEST_CODE);
            return false;
        }
        return true;

    }

    private void isTokenExpired() {
        final int doctorid = SpUtils.getInt(Contants.Id, 0);
        boolean isloginOut = SpUtils.getBoolean(Contants.logoOut, false);
        if (doctorid == 0) {
            if (isloginOut == true) {
                Log.d(TAG, "跳转登录" + 1);
                Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            } else {

                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }

        } else {
            VerifyToken();

        }

    }

    private void VerifyToken() {
      String token=  SpUtils.getString(Contants.Token);
        NetServices.Factory.getService().VerifyToken(token + ""
        ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseComPleteObserver() {
                    @Override
                    protected void onSuccees(BaseEntity t) throws Exception {
                        String logininfo = SpUtils.getString(Contants.LoginInfo);
                        MyApplication.LoginEntity = new Gson().fromJson(logininfo, Login.class);
                        MyApplication.isLogin = true;
                        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }

                    @Override
                    protected void onFailure(Throwable e, String erroressage, boolean isNetWorkError) throws Exception {
                        super.onFailure(e, erroressage, isNetWorkError);
                        Log.d(TAG, "跳转登录" + 2);
                        SpUtils.put(Contants.Id, 0);
                        SpUtils.put(Contants.logoOut, true);
                        SpUtils.put(Contants.toLogin, "0");
                        SpUtils.put(Contants.LoginJson, "");
                        Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                        startActivity(intent);
                        finish();
                    }
                });
    }

}